#include<bits/stdc++.h>
#define sd(n) scanf("%d",&n)
using namespace std;
//----------------------------------------------------------------------------//
typedef pair<int, int> PII;
const int N = 1010;

int n, m, k;
char g[N][N];
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
vector<PII> v1;
vector<PII> v2;
void bfs(int sx, int sy)
{
    for (int i = 0; i < 4; i++)
    {
        int x = sx + dx[i], y = sy + dy[i];
        if (x < 0 || x >= n || y < 0 || y >= n) continue;  // 出界
        g[x][y] = 'g';
        v2.push_back({ x,y });
    }
}

int main()
{
    sd(n); sd(m);
    //for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> g[i][j];
            if (g[i][j] == 'g') v1.push_back({i,j});
        }
    }
    sd(k);

    while (k--) 
    {
        for (auto& k2 : v1) bfs(k2.first, k2.second);
        v1 = v2;
        v2.clear();
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++) cout << g[i][j];
        puts("");
    }

    return 0;
}